package com.phonegap.extensions;

public class Command {

	private String name;
	private String prefix;
	private java.util.Hashtable<String, String> properties;
	private String url;
	private String parameters;
	
	
	
	
	public Command(String name, String prefix) {
		properties = new java.util.Hashtable<String, String>();
		this.name = name;
		this.prefix = prefix;
	}
	
	public String getName() {
		return name;
	}
	
	public String getPrefix() {
		return prefix;
		
	}
	
	public String getProperty(String propertyName) {
		return properties.get(propertyName);
		
	}
	
	
	public void setProperty(String propertyName, String propertyValue) {
		properties.put(propertyName, propertyValue);
	}
	
	public static Command parseCommand(String commandTag){
		
		commandTag = commandTag.replace("/>", "");
		commandTag = commandTag.replace("<", "");
		
		
		String[] commandProperties = commandTag.split(" ");		

		String name; String prefix;
		
		name = "undefined";
		prefix = "phonegap";
		
		if(commandProperties.length > 0) {
			String[] commandIdentifier = commandProperties[0].split(":");		
			if(commandProperties.length > 1) {
				name = commandIdentifier[1].trim();
			}
		}
		
		
		Command cmd = new Command(name, prefix);
		
		
		for(String property:commandProperties) {
			property = property.trim();
			
			if(property.length() == 0){
				continue;
			}
			
			String[] arrayProperty = property.split("=");		
			
			if(arrayProperty.length>1) {
				String propertyName  = arrayProperty[0];
				String propertyValue = arrayProperty[1];
				propertyValue = propertyValue.replace("\"", "");
				
				cmd.setProperty(propertyName, propertyValue);
				
				
			}
			
		}
		
		
		
		return cmd;
		
	}
	
	
}
